/*
 * File:   main.cpp
 * Author: ben
 *
 * Created on November 2, 2012, 1:25 PM
 */

#include <cstdlib>
#include <stdio.h>
#include <iostream>
#include "Random.h"
#include "Agent.h"
#include "Environment.h"

#define NUMBER_THREADS 1

using namespace std;

void printIO() {
    cout << "main: Enter command:" << endl;
    cout << "    0) Exit" << endl;
    cout << "    1) Print Environment" << endl;
    cout << "    2) Pause Agent threads" << endl;
    cout << "    3) Resume Agent threads" << endl;
    cout << "    4) Terminate all Agent threads" << endl;
    cout << "    5) Count Particles" << endl;
    cout << "    6) Account for Particles" << endl;
    cout << "    7) Toggle sleep for agents" << endl;
    cout << "    8) Print current locations of agents" << endl;
    cout << "Enter command: ";
}

int main(int argc, char** argv) {
    Synch* mySynch = new Synch();
    Environment* myEnvironment = new Environment();
    char threadName[100];
    bool done = false;
    Random::initSeed();
    // start threads
    Agent* agent1 = new Agent("agent1", mySynch, 
            Random::getRandomNum(BOARD_ROWS), Random::getRandomNum(BOARD_COLUMNS));

    Agent* agent2 = new Agent("agent2", mySynch, 
            Random::getRandomNum(BOARD_ROWS), Random::getRandomNum(BOARD_COLUMNS));
    Agent* agent3 = new Agent("agent3", mySynch,
            Random::getRandomNum(BOARD_ROWS), Random::getRandomNum(BOARD_COLUMNS));
    Agent* agent4 = new Agent("agent4", mySynch, 
            Random::getRandomNum(BOARD_ROWS), Random::getRandomNum(BOARD_COLUMNS));

    agent1->start();
    agent2->start();
    agent3->start();
    agent4->start();

    while (!done) {
        printIO();
        int cmd;
        cin >> cmd;

        switch (cmd) { // exit
            case 0:
            {
                done = true;
                break;
            }
            case 1: //Print Environment
            {
                myEnvironment->printBoard();
                break;
            }
            case 2: //Pause Agent Threads
            {
                mySynch->pauseThreads();
                break;
            }
            case 3: // Resume Agent Threads
            {
                mySynch->resumeThreads();
                break;
            }
            case 4: //Terminate all agent threads
            {
                
                mySynch->terminateThreads();
                break;
            }
            case 5: //Count Particles
            {
                cout << "Total starting particles: " << TOTAL_STARTING_PARTICLES
                        << endl;
                int particleCount = myEnvironment->countParticles();
                cout << "Total current particles: " << particleCount << endl;
                break;
            }
            case 6: //Account Particles
            {
                bool result = myEnvironment->accountParticles();
                if(result){
                    printf("All particles accounted for!\n");
                }
                else
                {
                    printf("Account failed\n");
                }
                break;
            }
            case 7: //Toggle sleep for agents
            {
                if(mySynch->sleep()){
                    mySynch->unsleepThreads();
                }
                else{
                    mySynch->sleepThreads();
                }
                break;
            }
            case 8: // print current location of agents
            {
                break; //Is this the extra credit?
            }
        }
    }

    return 0;
}


